export default class BinaryTreeNode {
  constructor(value = null) {
    this.left = null;
    this.right = null;
    this.parent = null;
    this.value = value;
  }
  setLeft(node) {
    // Reset parent for left node since it is going to be detached.
    if (this.left) {
      this.left.parent = null;
    }
    // Attach new node to the left.
    this.left = node;
    // Make current node to be a parent for new left one.
    if (this.left) {
      this.left.parent = this;
    }
    return this;
  }
  setRight(node) {
    // Reset parent for right node since it is going to be detached.
    if (this.right) {
      this.right.parent = null;
    }
    // Attach new node to the right.
    this.right = node;
    // Make current node to be a parent for new right one.
    if (node) {
      this.right.parent = this;
    }
    return this;
  }

  traverseInOrder() {
    let traverse = [];
    // Add left node.
    if (this.left) {
      traverse = traverse.concat(this.left.traverseInOrder());
    }
    // Add root.
    traverse.push(this.value);
    // Add right node.
    if (this.right) {
      traverse = traverse.concat(this.right.traverseInOrder());
    }
    return traverse;
  }
  toString() {
    // return this.traverseInOrder().toString();
    const traverse = this.traverseInOrder();
    const traverseId = traverse.map(item=> item.vid);
    return traverseId;
  }
}